﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using ARR.Entities;
using ARR.UI.AttributesService;
using DDF.Entity;
using ARR.UI.CategoriesService;

namespace ARR.UI.Models
{
    public interface IAttributesMasterModel : IModelBase
    {
        ObservableCollection<AttributeType> AttributeTypes { get; set; }

        void Save();
        void LoadAttributeTypes();

        List<Category> Categories { get; set; }
    }

    public class AttributesMasterModel : ModelBase, IAttributesMasterModel
    {
        public AttributesMasterModel()
        {
            AttributeTypes = new ObservableCollection<AttributeType>();

            LoadCategories();
            LoadAttributeTypes();
        }
        
        
        public ObservableCollection<AttributeType> AttributeTypes { get; set; }

        public List<Category> Categories { get; set; }


        private void LoadCategories()
        {
            CategoriesServiceClient service = new CategoriesServiceClient();

            try
            {
                var categories = service.GetByLevels(2);
                foreach (var category in categories.Where(x => x.Level == 2))
                {
                    category.SubCategories = new List<Category>();
                    category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
                }
                Categories = categories.Where(x => x.Level == 2).OrderBy(x => x.Name).ToList();

                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void LoadAttributeTypes()
        {
            AttributesServiceClient service = new AttributesServiceClient();
                
            try
            {
                AttributeTypes = new ObservableCollection<AttributeType>(service.GetAllTypesWithValues());
                service.Close();
            }
            catch
            {
                service.Abort();
                throw;
            }
        }

        public void Save()
        {
            var toSave = AttributeTypes.Where(a => a.GetEntityState() != EntityState.Original);

            if (toSave.Any())
            {
                AttributesServiceClient service = new AttributesServiceClient();

                try
                {
                    service.Save(AttributeTypes.ToList());
                    service.Close();
                }
                catch
                {
                    service.Abort();
                    throw;
                }
            }
        }
    }

    public class AttributesMasterModelMock : MockModelBase, IAttributesMasterModel
    {
        public AttributesMasterModelMock()
        {
            AttributeTypes = new ObservableCollection<AttributeType>();

            LoadAttributeTypes();
        }

        public ObservableCollection<AttributeType> AttributeTypes { get; set; }

        public List<Category> Categories { get; set; }


        public void LoadAttributeTypes()
        {
            AttributeTypes.Add(new AttributeType { Id = 1, Name = "ConsumptionType", Values = new List<Entities.Attribute>() });
            AttributeTypes.Add(new AttributeType { Id = 2, Name = "SizeSegment", Values = new List<Entities.Attribute>() });
            AttributeTypes.Add(new AttributeType { Id = 3, Name = "Flavour", Values = new List<Entities.Attribute>() });

            AttributeTypes[0].Values.Add(new Entities.Attribute { Id = 1, AttributeTypeId = 1, AttributeValue = "Immediate" });
            AttributeTypes[0].Values.Add(new Entities.Attribute { Id = 2, AttributeTypeId = 1, AttributeValue = "Future" });

            AttributeTypes[1].Values.Add(new Entities.Attribute { Id = 3, AttributeTypeId = 2, AttributeValue = "Single" });
            AttributeTypes[1].Values.Add(new Entities.Attribute { Id = 4, AttributeTypeId = 2, AttributeValue = "Multipack" });

            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 5, AttributeTypeId = 3, AttributeValue = "Apple" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 6, AttributeTypeId = 3, AttributeValue = "Chocolate" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 7, AttributeTypeId = 3, AttributeValue = "Almond" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 8, AttributeTypeId = 3, AttributeValue = "Strawberry" });
            AttributeTypes[2].Values.Add(new Entities.Attribute { Id = 9, AttributeTypeId = 3, AttributeValue = "Honey" });
        }

        public void Save()
        {
        }
    }
}
